/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import java.net.URI;
import jpa.entities.Pedido;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import jpa.entities.ItemPedido;
import jpa.entities.Usuario;
import java.util.Collection;

/**
 *
 * @author carlos
 */

@XmlRootElement(name = "pedido")
public class PedidoConverter {
    private Pedido entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of PedidoConverter */
    public PedidoConverter() {
        entity = new Pedido();
    }

    /**
     * Creates a new instance of PedidoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public PedidoConverter(Pedido entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getIdPedido() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getUsuario();
        getItemPedidoCollection();
    }

    /**
     * Creates a new instance of PedidoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public PedidoConverter(Pedido entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for idPedido.
     *
     * @return value for idPedido
     */
    @XmlElement
    public Integer getIdPedido() {
        return (expandLevel > 0) ? entity.getIdPedido() : null;
    }

    /**
     * Setter for idPedido.
     *
     * @param value the value to set
     */
    public void setIdPedido(Integer value) {
        entity.setIdPedido(value);
    }

    /**
     * Getter for usuario.
     *
     * @return value for usuario
     */
    @XmlElement
    public UsuarioConverter getUsuario() {
        if (expandLevel > 0) {
            if (entity.getUsuario() != null) {
                return new UsuarioConverter(entity.getUsuario(), uri.resolve("usuario/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for usuario.
     *
     * @param value the value to set
     */
    public void setUsuario(UsuarioConverter value) {
        entity.setUsuario((value != null) ? value.getEntity() : null);
    }

    /**
     * Getter for itemPedidoCollection.
     *
     * @return value for itemPedidoCollection
     */
    @XmlElement
    public ItemPedidoesConverter getItemPedidoCollection() {
        if (expandLevel > 0) {
            if (entity.getItemPedidoCollection() != null) {
                return new ItemPedidoesConverter(entity.getItemPedidoCollection(), uri.resolve("itemPedidoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for itemPedidoCollection.
     *
     * @param value the value to set
     */
    public void setItemPedidoCollection(ItemPedidoesConverter value) {
        entity.setItemPedidoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Pedido entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Pedido getEntity() {
        if (entity.getIdPedido() == null) {
            PedidoConverter converter = UriResolver.getInstance().resolve(PedidoConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Pedido entity.
     *
     * @return an resolved entity
     */
    public Pedido resolveEntity(EntityManager em) {
        Usuario usuario = entity.getUsuario();
        if (usuario != null) {
            entity.setUsuario(em.getReference(Usuario.class, usuario.getIdUsuario()));
        }
        Collection<ItemPedido> itemPedidoCollection = entity.getItemPedidoCollection();
        Collection<ItemPedido> newitemPedidoCollection = new java.util.ArrayList<ItemPedido>();
        if (itemPedidoCollection != null) {
            for (ItemPedido item : itemPedidoCollection) {
                newitemPedidoCollection.add(em.getReference(ItemPedido.class, item.getItemPedidoPK()));
            }
        }
        entity.setItemPedidoCollection(newitemPedidoCollection);
        return entity;
    }
}
